﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Common;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.EntitiesInfo;
using ConvertUtility = Lib.Utility.ConvertUtility;

namespace Web.Cms.Game
{
    public partial class CreateEditGame : AdminUserControl
    {
        #region Khai báo
        private int idGame;
        private CMSGameInfo info;
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                idGame = ConvertUtility.ToInt32(Request.QueryString["idGame"]) == null ? 0 : ConvertUtility.ToInt32(Request.QueryString["idGame"]);               

                txtDescription.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtDescription.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));




                if (!Page.IsPostBack)
                {
                    //Fill dữ liệu thuộc câu lạc bộ                  
                    dropZoneId.Items.Clear();
                    CMSZonesBL.GetDataComboType(dropZoneId, "0", "(" + ModulTypeBL.Game.Type.ToString() + ")");                   
                    txtCreatedOn.SelectedDate = DateTime.Now;
                    LoadData();
                }
            }
            catch (Exception ex)
            {
            }


        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (idGame > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;

                    info = CMSGameBL.GetInfo(2, idGame.ToString(),"");
                    if (info != null)
                    {
                        txtNameGame.Text = info.NameGame;
                        ImgAvatar.ImageUrl = info.Avatar;
                        ImgAvatar.Visible = true;
                        txtCreatedOn.SelectedDate = info.CreatedOn;
                        txtDescription.Text = info.Description;
                        txtAvatar.Text = info.Avatar;
                        txtContents.Text = info.Contents;
                        txtProduction.Text = info.Production;
                        txtLinkGame.Text = info.LinkGame;
                        dropZoneId.SelectedValue = info.CateId.ToString();



                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                    ImgAvatar.Visible = true;

                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }
        #endregion

        #region FillData
        void FillData(CMSGameInfo info)
        {

            info.IdGame = idGame;
            info.NameGame = txtNameGame.Text;
            info.CateId =ConvertUtility.ToInt32(dropZoneId.SelectedValue);
            info.LinkGame = txtLinkGame.Text;
            info.Production = txtProduction.Text;
            info.CreatedOn = txtCreatedOn.SelectedDate;
            info.CreatedBy = CurrentUser.Username;
            info.Status = 1;
            info.ShowType = 0;
            info.Description = txtDescription.Text;
            info.Contents = txtContents.Text;
            info.Avatar = txtAvatar.Text.Trim();
        }
        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                info = new CMSGameInfo();
                FillData(info);
                if (txtNameGame.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên game?");
                    txtNameGame.Focus();
                    return;
                }
                if (dropZoneId.SelectedIndex == 0)
                {

                    MessageBox.Show("Bạn chưa chọn chuyên mục cho game?");
                    dropZoneId.Focus();
                    return;
                }
                else
                {
                    int i = CMSGameBL.Insert(info);
                    if (i > 0)
                    {
                        Response.Write("/cms/Default.aspx?ctrlkey=ListGamePublic");
                    }
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                info = new CMSGameInfo();
                FillData(info);
                if (txtNameGame.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên game?");
                    txtNameGame.Focus();
                    return;
                }
                if(dropZoneId.SelectedIndex==0)
                {
                        MessageBox.Show("Bạn chưa chọn chuyên mục cho game?");
                      dropZoneId.Focus();
                        return; 
                }
                else
                {
                    CMSGameBL.Update(info);
                    Response.Redirect("/cms/Default.aspx?ctrlkey=ListGamePublic");
                    
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }

        }

        protected void btnCanel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/cms/Default.aspx?ctrlkey=ListGamePublic");
        }

        public void ClearText()
        {
            txtNameGame.Text = "";
            txtLinkGame.Text = "";
            txtProduction.Text = "";
            txtAvatar.Text = "";
            txtDescription.Text = "";
            txtContents.Text = "";
           
        }
    }
}